// pages/detail/detail.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        newsDetail: {},
        isStar: -1,
        userInfo: {},
        isInput: 0,
        commentTemp: "",
        commentBoxValue: "",
        //半屏菜单
        show: false,
        showActionsheet: false,
        //删除评论的actionsheet
        commentDeleteTemp: "",
        showActionsheet: false,
        groups: [{
            text: '删除',
            type: 'warn',
            value: 1
        }]
    },

    getCommentText: function (e) {
        this.setData({
            commentTemp: e.detail.value
        })
    },

    submitComment: function () {
        wx.showLoading({
            title: '正在提交',
        })
        wx.cloud.callFunction({
            name: "addComment",
            data: {
                artId: this.data.newsDetail._id,
                content: this.data.commentTemp
            }
        }).then(res => {
            wx.showToast({
                title: '评论成功',
            })
            this.getNewsDetail(this.data.newsDetail._id)
            this.setData({
                commentBoxValue: ""
            })
        })
        
    },

    getNewsDetail(id){
        var that = this
        wx.cloud.callFunction({
            name: 'getNewsDetail',
            data: {
                newsId: id
            }
        }).then(res => {
            wx.hideLoading()
            console.log(res.result)
            that.setData({
                newsDetail: res.result,
                isStar: res.result.isStar
            })
        })
    },

    getAllComment:function(){
        var that = this

        wx.cloud.callFunction({
            name: 'getAllComment',
            data: {
                newsId: that.data.newsDetail._id,
            }
        }).then(res => {
            wx.hideLoading()
            that.setData({
                allComment: res.result
            })
        })

    },

    //半屏
    open: function () {
        this.setData({
            show: true
        })
        wx.showLoading({
            title: '加载中',
            mask: true,
          })
          this.getAllComment()
    },

    //删除评论
    deleteComment: function (e) {
        console.log(e.currentTarget.dataset)
        if (e.currentTarget.dataset.isme) {
            this.setData({
                showActionsheet: true,
                commentDeleteTemp: e.currentTarget.dataset.commentid
            })
        }
    },

    cancelDelete(e){
        console.log("bushanchule")
        this.setData({
          commentDeleteTemp: ""
        })
      },
      close: function () {
        this.setData({
          showActionsheet: false
        })
      },
      btnClick(e) {
        var that = this
        if(e.detail.value) {
          console.log("delete "+this.data.commentDeleteTemp)
          wx.showLoading({
            title: '删除中',
            mask: true,
          })
          //云函数 删除
          wx.cloud.callFunction({
            name: "deleteComment",
            data:{
              commentId: that.data.commentDeleteTemp
            }
          }).then(res => {
            console.log(res)
            this.getAllComment()
            wx.showToast({
              title: '已删除',
              icon: "success",
              duration: 2000
            })
          })
        }
        this.setData({
          commentDeleteTemp: ""
        })
        this.close()
      },

      hsdclose:function(){
        console.log("souqipinlun")
        this.getNewsDetail(this.data.newsDetail._id)
      },
      //

    star: function () {
        var that = this
        that.setData({
            isStar: 1
        })
        wx.cloud.callFunction({
            name: "star",
            data: {
                _id: that.data.newsDetail._id
            }
        }).then(res => {
            console.log("star")
        })
    },

    unStar: function () {
        var that = this
        that.setData({
            isStar: 0
        })
        wx.cloud.callFunction({
            name: "unStar",
            data: {
                _id: that.data.newsDetail._id
            }
        }).then(res => {
            console.log("unStar")
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (e) {
        console.log(e)
        wx.showLoading({
            title: '加载中',
        })
        var that = this

        this.setData({
            userInfo: getApp().globalData.userInfo,
        })

       this.getNewsDetail(e.id)

        wx.cloud.callFunction({
            name: "readHistory",
            data: {
                _id: e.id
            }
        }).then(res => {
            console.log("已读++")
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})